#include "syscall.h"
#include "user.h"

#define Dim 	20	/* sum total of the arrays doesn't fit in 
			 * physical memory 
			 */
int A[Dim][Dim];
int B[Dim][Dim];
int C[Dim][Dim];

int D[Dim][Dim];
int E[Dim][Dim];
int F[Dim][Dim];

int X[1024];
int Y[1024];
   
void matTest1();
void matTest2();
void sortTest1();
void sortTest2();


int main(){
	int whichTest, matmultNO, sortNO, i;
	Printf("\n---------------------THIS IS TEST FOR PROJECT 3 PART 2 TEST:-----------------------");
	Printf("\n1->exec multiple matmults");
	Printf("\n2->exec multiple sorts");
	Printf("\n3->fork 2 matmults");
	Printf("\n4->fork 2 sorts");
	Printf("\nPlease enter 1,2,3,4\n");
	/*Printf("\n1 is choosed");*/
	whichTest = InputInt();
	switch(whichTest){
		case 1:
		Printf("\nPlease input the number of matmults you want to exec\n");
		matmultNO = InputInt();
		/*matmultNO = 1;*/
		for(i=1;i<=matmultNO;i++){
			Printf("\nExecing the %dth matmult....\n",i);
			Exec("../test/matmult",sizeof("../test/matmult"));
		}
		break;
		
		case 2:
		Printf("\nPlease input the number of sorts you want to exec\n");
		sortNO = InputInt();
		for(i=1;i<=sortNO;i++){
			Printf("\nExecing the %dth sort....\n",i);
			Exec("../test/sort",sizeof("../test/sort"));
		}
		break;
		case 3:
		/*Printf("\nPlease input the number of matmults you want to fork");*/
		Printf("\nForking 2 matmult...\n");
		/*matmultNO = InputInt();*/
/*		for(i=1;i<=matmultNO;i++){
*			Printf("\nForking the %dth sort....",i);
*			Fork(matTest);
*		}
*/
		Printf("\nForking 1st matmult..\n");
		Fork(matTest1);
		Printf("\nForking 2nd matmult..\n");
		Fork(matTest2);
		break;
		case 4:
		/*Printf("\nPlease input the number of sorts you want to fork");*/
		Printf("\nForking 2 sort...\n");
		/*sortNO = InputInt();*/
		/*for(i=1;i<=sortNO;i++){
			Printf("\nExecing the %dth sort....",i);
			Fork(sortTest);
		}*/
		
		Printf("\nForking 1st sort..\n");
		Fork(sortTest1);
		Printf("\nForking 2nd sort..\n");
		Fork(sortTest2);
		break;		
		default:
		Printf("\nWrong input, please re-run the program.");
	}
	return 0;
}

void matTest1(){
	int i, j, k;
    for (i = 0; i < Dim; i++)		/* first initialize the matrices */
	for (j = 0; j < Dim; j++) {
	     A[i][j] = i;
	     B[i][j] = j;
	     C[i][j] = 0;
	}

    for (i = 0; i < Dim; i++)		/* then multiply them together */
	for (j = 0; j < Dim; j++)
            for (k = 0; k < Dim; k++)
		 C[i][j] += A[i][k] * B[k][j];
	Printf("\n\nThe C[Dim-1][Dim-1] is %d\n\n",C[Dim-1][Dim-1]);
    Exit(0);		/* and then we're done */
}
void matTest2(){
	int a, b, c;
    for (a = 0; a < Dim; a++)		/* first initialize the matrices */
	for (b = 0; b < Dim; b++) {
	     D[a][b] = a;
	     E[a][b] = b;
	     F[a][b] = 0;
	}

    for (a = 0; a < Dim; a++)		/* then multiply them together */
	for (b = 0; b < Dim; b++)
            for (c = 0; c < Dim; c++)
		 F[a][b] += D[a][c] * E[c][b];
	Printf("\n\nThe F[Dim-1][Dim-1] is %d\n\n",F[Dim-1][Dim-1]);
    Exit(0);		/* and then we're done */
}

void sortTest1(){
    int i, j, tmp;

    /* first initialize the array, in reverse sorted order */
    for (i = 0; i < 1024; i++)		
        X[i] = 1024 - i;

    /* then sort! */
    for (i = 0; i < 1023; i++)
        for (j = i; j < (1023 - i); j++)
	   if (X[j] > X[j + 1]) {	/* out of order -> need to swap ! */
	      tmp = X[j];
	      X[j] = X[j + 1];
	      X[j + 1] = tmp;
    	   }
	Printf("DEBUG: the A[0] is %d\n",X[0]);
    Exit(0);		/* and then we're done -- should be 0! */


}
void sortTest2() {
	int i, j, tmp;

    /* first initialize the array, in reverse sorted order */
    for (i = 0; i < 1024; i++)		
        Y[i] = 1024 - i;

    /* then sort! */
    for (i = 0; i < 1023; i++)
        for (j = i; j < (1023 - i); j++)
	   if (Y[j] > Y[j + 1]) {	/* out of order -> need to swap ! */
	      tmp = Y[j];
	      Y[j] = Y[j + 1];
	      Y[j + 1] = tmp;
    	   }
	Printf("DEBUG: the A[0] is %d\n",Y[0]);
    Exit(0);		/* and then we're done -- should be 0! */


}
